package me.gm.stcont.validator;

import me.gm.stcont.model.User;

import org.apache.log4j.Logger;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

public class UserValidator implements Validator {

	private Logger logger;

	public UserValidator() {
		this.logger = Logger.getLogger(this.getClass());
	}

	public boolean supports(Class<?> clazz) {
		return User.class.equals(clazz);
	}

	public void validate(Object target, Errors errors) {
		logger.debug("started validate");

		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
				"user.userName.empty");

		User user;

		user = (User) target;

		if (user.getId().intValue() == 0) {
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
					"user.password.empty");
		}

		logger.debug("finished validate");
	}

}
